package com.example.demo.controller;

import com.alibaba.excel.EasyExcel;
import com.example.demo.ao.BulkAo;
import com.example.demo.model.DataAnylyseDao;
import com.example.demo.service.BulkInfoService;
import com.example.demo.vo.BulkVo;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/bulkinfo")
@Slf4j
public class BulkInfoListController {

    @Autowired
    private BulkInfoService bulkInfoService;

    @PostMapping("/getBulkInfoList")
    public Map<String, List<BulkVo>> getBulkInfoList(@RequestBody BulkAo bulkAo){
        Map<String, List<BulkVo>> bulkInfoList = bulkInfoService.getBulkInfoList(bulkAo);
        return bulkInfoList;
    }



    @GetMapping("/downLoadBulkInfoList")
    public void downLoadBulkInfoList(HttpServletResponse response, BulkAo bulkAo) throws IOException {
        Map<String, List<BulkVo>> bulkInfoMap = bulkInfoService.getBulkInfoList(bulkAo);
        List<BulkVo> objects = new ArrayList<>();
        bulkInfoMap.forEach((type,list)->{
            objects.addAll(list);
        });
        String fileName = "中文.xlsx";
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
//        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
        EasyExcel.write(response.getOutputStream(), BulkVo.class).sheet("Sheet1").doWrite(objects);
    }



    @PostMapping("/infoList")
    public List<BulkVo> infoList(HttpServletResponse response,BulkAo bulkAo) {
        Map<String, List<BulkVo>> bulkInfoMap = bulkInfoService.getBulkInfoList(bulkAo);
        List<Map.Entry<String,List<BulkVo>>> entryList = new ArrayList<>(bulkInfoMap.entrySet());
        List<String> data = new ArrayList<>();
        List<BulkVo> alist = new ArrayList<>();
        for (Map.Entry<String, List<BulkVo>> stringListEntry : entryList) {
            String key = stringListEntry.getKey();
            List<BulkVo> value = stringListEntry.getValue();
            alist.addAll(value);
            data.add(key);
        }
        System.out.println(alist);
        System.out.println(data);


        // 转换键为List
//        List<BulkVo> keysList = bulkInfoMap.keySet().stream().collect(Collectors.toList());

// 转换值为List
//        List<String> valuesList = bulkInfoMap.values().stream().collect(Collectors.toList());

// 转换键值对为List
//        List<Map.Entry<String, List<BulkVo>>> entryList = bulkInfoMap.entrySet().stream().collect(Collectors.toList());


//        List<Map.Entry<String,List<BulkVo>>> entryList = new ArrayList<>(bulkInfoMap.entrySet());
        return alist;
    }


    @PostMapping("/getBulkUserList")
    public String getBulkUserList(@RequestBody List<BulkAo> bulkAo) {
        int num = 0;
        String message = "成功";
        String error = "请检查";
        num = bulkInfoService.getBulkUserList(bulkAo);
        if (num != 0){
            return message;
        }else {
            return error;
        }
    }

}
